//
//  MWPhoneLoginController.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/21.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWPhoneLoginController.h"
#import "MWPasswordLoginController.h"
#import "MWRegisterController.h"
#import "MWImprovedInfoController.h"
#import "MWTabBarController.h"
#import "MWXRTabBarController.h"
#import "MWCountryCodeController.h"


#import "JKCountDownButton.h"

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>


#import "HSFTabBarController.h"

#import "MWNavigationController.h"
#import "MWHomeController.h"
#import "ShowRoomViewController.h"
#import "MWShopController.h"
#import "MWTreeSeedController.h"
#import "MWApplicationController.h"
#import "MWMineController.h"

#define kIs_iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define kIs_iPhoneX kScreenWidth >=375.0f && kScreenHeight >=812.0f&& kIs_iphone
/*TabBar高度*/
#define kTabBarHeight (CGFloat)(kIs_iPhoneX?(49.0 + 34.0):(49.0))

@interface MWPhoneLoginController () <MWWXApiManagerDelegate>

@property(nonatomic, strong) UIButton *backBtn;
@property(nonatomic, strong) UILabel *hintLB;
@property(nonatomic, strong) UIButton *phoneBtn;
@property(nonatomic, strong) UITextField *phoneTF;
@property(nonatomic, strong) UIView *phoneLineView;
@property(nonatomic, strong) UITextField *smsCodeTF;
@property(nonatomic, strong) UIView *smsCodeLineView;
@property(nonatomic, strong) JKCountDownButton *countDownButton;

@property(nonatomic, strong) UIButton *loginBtn;
@property(nonatomic, strong) UIButton *pwdLoginBtn;
@property(nonatomic, strong) UIButton *registerBtn;
@property(nonatomic, strong) UIButton *fbLoginBtn;
@property(nonatomic, strong) UIButton *wxLoginBtn;

@property(nonatomic, strong) NSString *countryCode;

@end

@implementation MWPhoneLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupPageSubviews];
    [self layoutPageSubviews];
    [self setLocalizable];
    [MWWXApiManager sharedManager].delegate = self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)loginAction:(UIButton *)sender {
    [self.view endEditing:YES];
    NSString *phone = _phoneTF.text;
    NSString *captcha = _smsCodeTF.text;
    
    [self loginWithPhone:phone captcha:captcha countryCode:_countryCode];
}

- (void)facebookLoginAction:(UIButton *)sender {
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithPermissions: @[@"public_profile"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
      if (error) {
        DLog(@"Failer");
          
      } else if (result.isCancelled) {
        DLog(@"Cancelled");
          
      } else {
          [self loginWithFaceBook:result.token.userID];
      }
    }];
}

- (void)weixinLoginAction:(UIButton *)sender {
    //构造SendAuthReq结构体
    SendAuthReq *req = [[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo";
    req.state = @"Wood";
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req completion:^(BOOL success) {
        
    }];
}

- (void)phoneAction:(UIButton *)sender {
    MWCountryCodeController *controller = [[MWCountryCodeController alloc] init];
    MWWeakSelf
    controller.countryCodeBlock = ^(NSString *countryCode) {
        weakSelf.countryCode = countryCode;
        [weakSelf.phoneBtn setTitle:[NSString stringWithFormat:@"+%@", countryCode] forState:UIControlStateNormal];
        [weakSelf.phoneBtn setIconInRightWithSpacing:5];
    };
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)managerDidRecvAuthResponse:(SendAuthResp *)response {
    if (response.errCode == 0) {
        [self loadWXAccessTokenWithCode:response.code];
    }
}

- (void)passwordLoginAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)registerAction:(UIButton *)sender {
    MWRegisterController *controller = [[MWRegisterController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 微信登录根据code获取access_token
- (void)loadWXAccessTokenWithCode:(NSString *)code {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:WXAppID forKey:@"appid"];
    [paraDic setObject:WXAppSecret forKey:@"secret"];
    [paraDic setObject:code forKey:@"code"];
    [paraDic setObject:@"authorization_code" forKey:@"grant_type"];
    
    NSString *url = @"https://api.weixin.qq.com/sns/oauth2/access_token";
    
    [[MWNetworkManager manager] POST:url parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        [self loginWithOpenid:[dic objectForKey:@"openid"]];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 微信登录
- (void)loginWithOpenid:(NSString *)openid {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:openid forKey:@"openid"];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    [[MWNetworkManager manager] POST:kWXLoginUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        if (code == 1) {
            [hud hideAnimated:YES];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            NSInteger status = [[dataDic objectForKey:@"status"] integerValue];
            if (status == 0) {
                MWImprovedInfoController *controller = [[MWImprovedInfoController alloc] init];
                controller.openid = [dataDic objectForKey:@"openid"];
                [self.navigationController pushViewController:controller animated:YES];
                
            } else {
                MWUser *user = [MWUser mj_objectWithKeyValues:[dataDic objectForKey:@"userinfo"]];
                [MWUserManager manager].user = user;
                [[MWUserManager manager] saveUser];

                if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
                    NSString *homeTitle = [[MWConfigManager manager] objectForKey:@"homepage" defaultValue:@"首页"];
                    NSString *showroomTitle = [[MWConfigManager manager] objectForKey:@"showroom" defaultValue:@"展厅"];
                    NSString *shopTitle = [[MWConfigManager manager] objectForKey:@"goods" defaultValue:@"商品"];
                    NSString *treeSeedTitle = [[MWConfigManager manager] objectForKey:@"treetype" defaultValue:@"树种"];
                    NSString *applicationTitle = [[MWConfigManager manager] objectForKey:@"application" defaultValue:@"应用"];
                    NSString *mineTitle = [[MWConfigManager manager] objectForKey:@"my" defaultValue:@"我的"];

                    MWNavigationController *navi1 = [[MWNavigationController alloc] initWithRootViewController:[[MWHomeController alloc] init]];
                    MWNavigationController *navi2 = [[MWNavigationController alloc] initWithRootViewController:[[ShowRoomViewController alloc] init]];
                    MWNavigationController *navi3 = [[MWNavigationController alloc] initWithRootViewController:[[MWShopController alloc] init]];
                    MWNavigationController *navi4 = [[MWNavigationController alloc] initWithRootViewController:[[MWTreeSeedController alloc] init]];
                    MWNavigationController *navi5 = [[MWNavigationController alloc] initWithRootViewController:[[MWApplicationController alloc] init]];
                    MWNavigationController *navi6 = [[MWNavigationController alloc] initWithRootViewController:[[MWMineController alloc] init]];
                    HSFTabBarController *rootController = [[HSFTabBarController alloc] init];
                    rootController.childVCArr = @[navi1, navi2, navi3, navi5, navi4, navi6];
                    rootController.source = @[@{@"title":homeTitle, @"selImg":@"tab_home_s", @"norImg":@"tab_home_n"},
                    @{@"title":showroomTitle, @"selImg":@"tab_showroom_s", @"norImg":@"tab_showroom_n"},
                    @{@"title":shopTitle, @"selImg":@"tab_shop_s", @"norImg":@"tab_shop_n"},
                                @{@"title":applicationTitle, @"selImg":@"tab_application_s", @"norImg":@"tab_application_n"},
                                              @{@"title":treeSeedTitle, @"selImg":@"tab_tree_seed_s", @"norImg":@"tab_tree_seed_n"},

                    @{@"title":mineTitle, @"selImg":@"tab_mine_s", @"norImg":@"tab_mine_n"}];
                    rootController.norColor = MWHexColor(@"#666666");
                    rootController.selColor = MWHexColor(@"#333333");
                    rootController.loadAll = YES;//是否一次全部加载
                    rootController.canScroll = NO;
                    rootController.tabBarHeight = kTabBarHeight;
                    rootController.style = HSFTabBarStyle_baseline;
                    rootController.baseline_color = MWHexColor(@"#333333");
                    rootController.isHaveTopline = NO;
                    [rootController setUp];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = rootController;
                    [MWUserManager manager].tbc = rootController;

                } else {
                    MWXRTabBarController *controller = [[MWXRTabBarController alloc] init];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = controller;
                }
            }
            
        } else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = msg;
            [hud hideAnimated:YES afterDelay:2];
        }
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}

#pragma mark - FaceBook登录
- (void)loginWithFaceBook:(NSString *)fackbookId {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:fackbookId forKey:@"fackbook_id"];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    [[MWNetworkManager manager] POST:kFacebookLoginUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        if (code == 1) {
            [hud hideAnimated:YES];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            NSInteger status = [[dataDic objectForKey:@"status"] integerValue];
            if (status == 0) {
                MWImprovedInfoController *controller = [[MWImprovedInfoController alloc] init];
                controller.fackbookId = [dataDic objectForKey:@"facebook"];
                [self.navigationController pushViewController:controller animated:YES];
                
            } else {
                MWUser *user = [MWUser mj_objectWithKeyValues:[dataDic objectForKey:@"userinfo"]];
                [MWUserManager manager].user = user;
                [[MWUserManager manager] saveUser];

                if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
                    NSString *homeTitle = [[MWConfigManager manager] objectForKey:@"homepage" defaultValue:@"首页"];
                    NSString *showroomTitle = [[MWConfigManager manager] objectForKey:@"showroom" defaultValue:@"展厅"];
                    NSString *shopTitle = [[MWConfigManager manager] objectForKey:@"goods" defaultValue:@"商品"];
                    NSString *treeSeedTitle = [[MWConfigManager manager] objectForKey:@"treetype" defaultValue:@"树种"];
                    NSString *applicationTitle = [[MWConfigManager manager] objectForKey:@"application" defaultValue:@"应用"];
                    NSString *mineTitle = [[MWConfigManager manager] objectForKey:@"my" defaultValue:@"我的"];

                    MWNavigationController *navi1 = [[MWNavigationController alloc] initWithRootViewController:[[MWHomeController alloc] init]];
                    MWNavigationController *navi2 = [[MWNavigationController alloc] initWithRootViewController:[[ShowRoomViewController alloc] init]];
                    MWNavigationController *navi3 = [[MWNavigationController alloc] initWithRootViewController:[[MWShopController alloc] init]];
                    MWNavigationController *navi4 = [[MWNavigationController alloc] initWithRootViewController:[[MWTreeSeedController alloc] init]];
                    MWNavigationController *navi5 = [[MWNavigationController alloc] initWithRootViewController:[[MWApplicationController alloc] init]];
                    MWNavigationController *navi6 = [[MWNavigationController alloc] initWithRootViewController:[[MWMineController alloc] init]];
                    HSFTabBarController *rootController = [[HSFTabBarController alloc] init];
                    rootController.childVCArr = @[navi1, navi2, navi3, navi5, navi4, navi6];
                    rootController.source = @[@{@"title":homeTitle, @"selImg":@"tab_home_s", @"norImg":@"tab_home_n"},
                    @{@"title":showroomTitle, @"selImg":@"tab_showroom_s", @"norImg":@"tab_showroom_n"},
                    @{@"title":shopTitle, @"selImg":@"tab_shop_s", @"norImg":@"tab_shop_n"},
                                @{@"title":applicationTitle, @"selImg":@"tab_application_s", @"norImg":@"tab_application_n"},
                                              @{@"title":treeSeedTitle, @"selImg":@"tab_tree_seed_s", @"norImg":@"tab_tree_seed_n"},

                    @{@"title":mineTitle, @"selImg":@"tab_mine_s", @"norImg":@"tab_mine_n"}];
                    rootController.norColor = MWHexColor(@"#666666");
                    rootController.selColor = MWHexColor(@"#333333");
                    rootController.loadAll = YES;//是否一次全部加载
                    rootController.canScroll = NO;
                    rootController.tabBarHeight = kTabBarHeight;
                    rootController.style = HSFTabBarStyle_baseline;
                    rootController.baseline_color = MWHexColor(@"#333333");
                    rootController.isHaveTopline = NO;
                    [rootController setUp];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = rootController;
                    [MWUserManager manager].tbc = rootController;
                } else {
                    MWXRTabBarController *controller = [[MWXRTabBarController alloc] init];
                    UIApplication *application = [UIApplication sharedApplication];
                    application.keyWindow.rootViewController = controller;
                }
            }
            
        } else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = msg;
            [hud hideAnimated:YES afterDelay:2];
        }
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}


#pragma mark - 登录验证码请求
- (void)loadSmsCodeWithPhone:(NSString *)phone countryCode:(NSString *)countryCode {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:phone forKey:@"mobile"];
    [paraDic setObject:countryCode forKey:@"country_code"];
    [paraDic setObject:@"mobilelogin" forKey:@"event"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MWWeakSelf
    [[MWNetworkManager manager] POST:kSmsCodeUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
    
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = msg;
        [hud hideAnimated:YES afterDelay:2];
        
        if (code == 1) {
            weakSelf.countDownButton.enabled = NO;
            [weakSelf.countDownButton startCountDownWithSecond:60];
        }
       
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 登录请求
- (void)loginWithPhone:(NSString *)phone captcha:(NSString *)captcha countryCode:(NSString *)countryCode {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:phone forKey:@"mobile"];
    [paraDic setObject:captcha forKey:@"captcha"];
    [paraDic setObject:countryCode forKey:@"country_code"];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    [[MWNetworkManager manager] POST:kPhoneLoginUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        if (code == 1) {
            [hud hideAnimated:YES];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            MWUser *user = [MWUser mj_objectWithKeyValues:[dataDic objectForKey:@"userinfo"]];
            [MWUserManager manager].user = user;
            [[MWUserManager manager] saveUser];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kLoginResultNotification object:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = msg;
            [hud hideAnimated:YES afterDelay:2];
        }
    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
    }];
}

- (void)setLocalizable {
    if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
        _hintLB.text = [[MWConfigManager manager] objectForKey:@"welcome_into_wood" defaultValue:@"欢迎来到我的木头"];
    } else {
        _hintLB.text = [[MWConfigManager manager] objectForKey:@"welcome_into_xiran" defaultValue:@"欢迎来到木头猎人"];
    }
    _phoneTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_phone" defaultValue:@"请输入手机号"];
    _smsCodeTF.placeholder = [[MWConfigManager manager] objectForKey:@"input_code" defaultValue:@"请输入验证码"];
    [_countDownButton setTitle:[[MWConfigManager manager] objectForKey:@"get_code" defaultValue:@"获取验证码"] forState:UIControlStateNormal];
    [_pwdLoginBtn setTitle:[[MWConfigManager manager] objectForKey:@"login_by_pwd" defaultValue:@"密码登录"] forState:UIControlStateNormal];
    [_loginBtn setTitle:[[MWConfigManager manager] objectForKey:@"login" defaultValue:@"登录"] forState:UIControlStateNormal];
   
    NSString *no_account = [[MWConfigManager manager] objectForKey:@"no_account" defaultValue:@"还没有账号？"];
    NSString *register_now = [[MWConfigManager manager] objectForKey:@"register_now" defaultValue:@"立即注册"];
    
    NSString *registerHint = [NSString stringWithFormat:@"%@%@",no_account, register_now];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:registerHint attributes: @{NSFontAttributeName: MWFont(13),NSForegroundColorAttributeName: MWHexColor(@"#666666")}];
    [string addAttributes:@{NSForegroundColorAttributeName: MWHexColor(@"#666666")} range:NSMakeRange(0, no_account.length)];
    [string addAttributes:@{NSForegroundColorAttributeName: MWHexColor(@"#FD9F2B")} range:NSMakeRange(no_account.length, register_now.length)];
    [_registerBtn setAttributedTitle:string forState:UIControlStateNormal];
    
    [_wxLoginBtn setTitle:[[MWConfigManager manager] objectForKey:@"wx" defaultValue:@"微信"]  forState:UIControlStateNormal];
    [_wxLoginBtn setIconInTopWithSpacing:10];
}

- (void)setupPageSubviews {
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateNormal];
    [_backBtn setImage:[UIImage imageNamed:@"ic_arrow_left"] forState:UIControlStateHighlighted];
    [_backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backBtn];
    
    _hintLB = [[UILabel alloc] init];
    _hintLB.font = MWBoldFont(22);
    _hintLB.numberOfLines = 2;
    _hintLB.textColor = MWHexColor(@"#1E1934");
    [self.view addSubview:_hintLB];
    
    _countryCode = @"86";
    _phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _phoneBtn.titleLabel.font = MWFont(14);
    [_phoneBtn setTitle:@"+86" forState:UIControlStateNormal];
    [_phoneBtn setTitleColor:MWHexColor(@"#1E1934") forState:UIControlStateNormal];
    [_phoneBtn setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    [_phoneBtn addTarget:self action:@selector(phoneAction:) forControlEvents:UIControlEventTouchUpInside];
    [_phoneBtn setIconInRightWithSpacing:5];
    [self.view addSubview:_phoneBtn];
    
    _phoneTF = [[UITextField alloc] init];
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _phoneTF.font = MWFont(14);
    _phoneTF.textColor = MWHexColor(@"#BAC2CE");
    [self.view addSubview:_phoneTF];
    
    _phoneLineView = [[UIView alloc] init];
    _phoneLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_phoneLineView];
    
    _smsCodeTF = [[UITextField alloc] init];
    _smsCodeTF.keyboardType = UIKeyboardTypeNumberPad;
    _smsCodeTF.font = MWFont(14);
    _smsCodeTF.textColor = MWHexColor(@"#BAC2CE");
    [self.view addSubview:_smsCodeTF];
    
    _smsCodeLineView = [[UIView alloc] init];
    _smsCodeLineView.backgroundColor = MWHexColor(@"#D8D8D8");
    [self.view addSubview:_smsCodeLineView];
    
    _countDownButton = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
    _countDownButton.titleLabel.font = MWFont(12);
    [_countDownButton setTitleColor:MWHexColor(@"#1E1934") forState:UIControlStateNormal];
    [self.view addSubview:_countDownButton];
    
    MWWeakSelf
    [_countDownButton countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
        NSString *phone = weakSelf.phoneTF.text;
        [self loadSmsCodeWithPhone:phone countryCode:_countryCode];
        
        [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
            NSString *title = [NSString stringWithFormat:@"%zds",second];
            return title;
        }];
        
        [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.enabled = YES;
            return [[MWConfigManager manager] objectForKey:@"getcode_retry" defaultValue:@"重新获取验证码"];
        }];
    }];
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBtn.layer.cornerRadius = 5;
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.titleLabel.font = MWFont(14);
    [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_loginBtn setBackgroundImage:[UIImage imageWithColor:MWHexColor(@"#1E1934")] forState:UIControlStateNormal];
    [_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginBtn];
    
    _pwdLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _pwdLoginBtn.titleLabel.font = MWFont(12);
    [_pwdLoginBtn setTitleColor:MWHexColor(@"#E53030") forState:UIControlStateNormal];
    [_pwdLoginBtn addTarget:self action:@selector(passwordLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_pwdLoginBtn];
    
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerBtn.titleLabel.font = MWFont(13);
    [_registerBtn addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerBtn];
    
    _fbLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _fbLoginBtn.hidden = YES;
    _fbLoginBtn.titleLabel.font = MWFont(12);
    [_fbLoginBtn setTitle:@"facebook" forState:UIControlStateNormal];
    [_fbLoginBtn setTitleColor:MWHexColor(@"#C2C6CD") forState:UIControlStateNormal];
    [_fbLoginBtn setImage:[UIImage imageNamed:@"login_fb"] forState:UIControlStateNormal];
    [_fbLoginBtn addTarget:self action:@selector(facebookLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_fbLoginBtn];
    
    _wxLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _wxLoginBtn.hidden = YES;
    _wxLoginBtn.titleLabel.font = MWFont(12);
    [_wxLoginBtn setTitle:@"微信" forState:UIControlStateNormal];
    [_wxLoginBtn setTitleColor:MWHexColor(@"#C2C6CD") forState:UIControlStateNormal];
    [_wxLoginBtn setImage:[UIImage imageNamed:@"login_wx"] forState:UIControlStateNormal];
    [_wxLoginBtn addTarget:self action:@selector(weixinLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_wxLoginBtn];
    
//    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
//        _wxLoginBtn.hidden = NO;
//    } else {
//        _wxLoginBtn.hidden = YES;
//    }
    
}

- (void)layoutPageSubviews {
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kStatusBarHeight + 15);
        make.left.equalTo(@16);
    }];
    
    [_hintLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kStatusBarHeight + 110);
        make.left.equalTo(self.view).offset(16);
        make.right.mas_lessThanOrEqualTo(-12);
    }];
    
    [_phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.centerY.equalTo(self.phoneTF);
        make.width.equalTo(@80);
        make.height.equalTo(@20);
    }];
    [_phoneBtn setIconInRightWithSpacing:5];
    
    [_phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hintLB.mas_bottom).offset(48);
        make.left.equalTo(self.phoneBtn.mas_right).offset(10);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_phoneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneTF.mas_bottom).offset(6);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_smsCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneLineView.mas_bottom).offset(23);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_countDownButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-16);
        make.centerY.equalTo(self.smsCodeTF);
    }];
    
    [_smsCodeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsCodeTF.mas_bottom).offset(6);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@0.5);
    }];
    
    [_pwdLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsCodeLineView.mas_bottom).offset(8);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.smsCodeLineView.mas_bottom).offset(77);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.height.equalTo(@44);
    }];
    
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginBtn.mas_bottom).offset(16);
        make.centerX.equalTo(self.view);
    }];
    
    [_fbLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-90);
        make.centerX.equalTo(self.view).offset(-45);
    }];
    [_fbLoginBtn setIconInTopWithSpacing:10];
    
    [_wxLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-90);
        make.centerX.equalTo(self.view).offset(45);
    }];
    [_wxLoginBtn setIconInTopWithSpacing:10];
}

@end
